﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using QXBook.Model;
using QXBook.Utils;
using System.Data.Common;
using System.Data.SqlClient;

namespace QXBook.Service
{
    public class ProductService : ServiceBase
    {
        public void DeleteList(string ids)
        {
            string sql = "DELETE FROM CoreTemplate WHERE CoreID IN (SELECT CoreID FROM Product WHERE ProductID IN (" + ids + "))";
            sql += "DELETE FROM Product WHERE ProductID IN (" + ids + "))";
            this.db.ExecuteNonQuery(CommandType.Text, sql);
        }

        public ProductInfo GetModel(int productID)
        {
            string sql = "SELECT A.Title,A.CategoryID,A.Author,A.SmallImg,A.BigImg,A.IsPublish,A.Content,A.AddDate,B.ProductID,B.CoreID,B.Price FROM CoreTemplate A JOIN Product B ON B.CoreID=A.CoreID WHERE B.ProductID=" + productID;

            DataSet ds = this.db.ExecuteDataSet(CommandType.Text, sql);

            if (ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            {
                ProductInfo product = new ProductInfo();
                foreach (DataRow dr in ds.Tables[0].Rows)
                {
                    product.ProductID = CommonUtils.ToInt(dr["ProductID"]);
                    product.CategoryID = CommonUtils.ToInt(dr["CategoryID"]);
                    product.CoreID = CommonUtils.ToInt(dr["CoreID"]);
                    //作家
                    product.Author = CommonUtils.ToString(dr["Author"]);
                    product.SmallImg = CommonUtils.ToString(dr["SmallImg"]);
                    product.BigImg = CommonUtils.ToString(dr["BigImg"]);
                    product.IsPublish = (YesNo)CommonUtils.ToInt(dr["IsPublish"]);
                    product.Title = CommonUtils.ToString(dr["Title"]);
                    product.Price = CommonUtils.ToDecimal(dr["Price"]);
                    product.Content = CommonUtils.ToString(dr["Content"]);
                    product.AddDate = CommonUtils.ToTime(dr["AddDate"].ToString());
                }

                return product;
            }
            return null;
        }

        public string AddProduct(string AdminName, ProductInfo article)
        {
            MembersInfo admininfo = ServiceFactory.CreateInstance<AdminService>().GetAdminEntityByAdminName(AdminName);
            //判断当前管理员是否存在
            string rtmessage = ServiceFactory.CreateInstance<AdminService>().CheckAdminPower(admininfo);
            if (!string.IsNullOrEmpty(rtmessage)) return rtmessage;

            DbCommand cmd = new SqlCommand();
            cmd.CommandText = @"INSERT INTO CoreTemplate(Title,CategoryID,Content,Author,SmallImg,BigImg,IsPublish,AddDate) 
                                VALUES (@Title,@CategoryID,@Content,@Author,@SmallImg,@BigImg,@IsPublish,GETDATE());SELECT @@IDENTITY";//返回主键

            base.db.AddInParameter(cmd, "@Title", DbType.String, article.Title);
            base.db.AddInParameter(cmd, "@CategoryID", DbType.String, article.CategoryID);
            base.db.AddInParameter(cmd, "@Content", DbType.String, article.Content);
            base.db.AddInParameter(cmd, "@Author", DbType.String, article.Author);
            base.db.AddInParameter(cmd, "@SmallImg", DbType.String, article.SmallImg);
            base.db.AddInParameter(cmd, "@BigImg", DbType.String, article.SmallImg);
            base.db.AddInParameter(cmd, "@IsPublish", DbType.Int16, (int)article.IsPublish);

            int identity = 0;
            object obj = base.db.ExecuteScalar(cmd);
            if (obj != null)
            {
                identity = CommonUtils.ToInt(obj);
            }

            cmd.CommandText = @"INSERT INTO Product(CoreID,Sales,FavoritCount,PublishHourse,Price) 
                                VALUES (@CoreID,0,0,@PublishHourse,@Price)";

            base.db.AddInParameter(cmd, "@CoreID", DbType.Int32, identity);
            base.db.AddInParameter(cmd, "@PublishHourse", DbType.String, article.PublishHourse);
            base.db.AddInParameter(cmd, "@Price", DbType.Decimal, article.Price);

            base.db.ExecuteNonQuery(cmd);
            return string.Empty;
        }

        public string UpdateProduct(string AdminName, ProductInfo product)
        {
            MembersInfo admininfo = ServiceFactory.CreateInstance<AdminService>().GetAdminEntityByAdminName(AdminName);
            //判断当前管理员是否存在
            string rtmessage = ServiceFactory.CreateInstance<AdminService>().CheckAdminPower(admininfo);
            if (!string.IsNullOrEmpty(rtmessage)) return rtmessage;

            string SQL = "UPDATE CoreTemplate SET ";
            SQL += "Title=@Title,CategoryID=@CategoryID,Content=@Content,Author=@Author,SmallImg=@SmallImg,BigImg=@BigImg,IsPublish=@IsPublish,UpdateDate=GETDATE() WHERE CoreID=@CoreID";

            DbCommand cmd = new SqlCommand(SQL);
            base.db.AddInParameter(cmd, "@Title", DbType.String, product.Title);
            base.db.AddInParameter(cmd, "@CategoryID", DbType.Int32, product.CategoryID);
            base.db.AddInParameter(cmd, "@Content", DbType.String, product.Content);
            base.db.AddInParameter(cmd, "@Author", DbType.String, product.Author);
            base.db.AddInParameter(cmd, "@SmallImg", DbType.String, product.SmallImg);
            base.db.AddInParameter(cmd, "@BigImg", DbType.String, product.BigImg);
            base.db.AddInParameter(cmd, "@IsPublish", DbType.Int16, (int)product.IsPublish);
            base.db.AddInParameter(cmd, "@CoreID", DbType.Int32, product.CoreID);
            base.db.ExecuteNonQuery(cmd);

            cmd.CommandText = @"UPDATE Product SET 
                    PublishHourse=@PublishHourse,Price=@Price WHERE ProductID=@ProductID";
            base.db.AddInParameter(cmd, "@PublishHourse", DbType.String, product.PublishHourse);
            base.db.AddInParameter(cmd, "@Price", DbType.Decimal, product.Price);
            base.db.AddInParameter(cmd, "@ProductID", DbType.Int32, product.ProductID);

            return string.Empty;
        }

        public List<ProductInfo> GetModelList(ref PagerState ps, ProductCondition condition)
        {
            List<ProductInfo> list = new List<ProductInfo>();
            DbCommand cmd = new SqlCommand();

            StringBuilder sbSql = new StringBuilder();
            sbSql.Append("SELECT A.ProductID,A.Price,B.Title,B.SmallImg,B.IsPublish,B.AddDate,B.UpDateDate,C.CategoryID,C.CategoryName FROM CoreTemplate B JOIN Product A ON B.CoreID=A.CoreID");
            sbSql.Append(" LEFT JOIN Category C ON B.CategoryID=C.CategoryID ");
            sbSql.Append(" WHERE 1=1");
            if (condition != null)
            {
                if (condition.IsPublish != YesNo.Unknow)
                {
                    sbSql.Append(" AND B.IsPublish=" + (int)condition.IsPublish);
                }
            }

            DataTable dt = this.GetListPager(ref ps, sbSql.ToString(), "ProductID", cmd);

            if (dt != null && dt.Rows.Count > 0)
            {
                foreach (DataRow dr in dt.Rows)
                {
                    ProductInfo pro = new ProductInfo();
                    pro.ProductID = CommonUtils.ToInt(dr["ProductID"]);
                    pro.CategoryID = CommonUtils.ToInt(dr["CategoryID"]);
                    pro.Title = CommonUtils.ToString(dr["Title"]);
                    pro.IsPublish = (YesNo)CommonUtils.ToInt(dr["IsPublish"]);
                    pro.AddDate = CommonUtils.ToTime(dr["AddDate"].ToString());
                    pro.UpdateDate = CommonUtils.ToTime(dr["UpdateDate"].ToString());

                    pro.Categorie = new CategoryInfo();
                    pro.Categorie.CategoryID = CommonUtils.ToInt(dr["CategoryID"]);
                    pro.Categorie.CategoryName = CommonUtils.ToString(dr["CategoryName"]);
                    list.Add(pro);
                }
            }

            return list;
        }
    }
}
